package com.nullpointer.xchatfinal.fragment;

import java.util.List;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

import com.nullpointer.xchatfinal.R;
import com.nullpointer.xchatfinal.utils.Common;
import com.nullpointer.xchatfinal.utils.KiraGameLog;
import com.nullpointer.xchatfinal.utils.Utils;
import com.quickblox.core.QBEntityCallbackImpl;
import com.quickblox.customobjects.QBCustomObjects;
import com.quickblox.customobjects.model.QBCustomObject;
import com.quickblox.customobjects.model.QBPermissions;
import com.quickblox.customobjects.model.QBPermissionsLevel;

public class FeedbackDialogFragment extends DialogFragment {
	private EditText etFeedback;
	private Button btPost;
	private Button btCancel;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_feedback, container);
		findViewIds(view);
		init();
		setEvent();
		return view;
	}

	private void init() {
		getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
		btPost.setEnabled(false);
	}

	private void setEvent() {
		btPost.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Utils.hideKeyboard(getActivity(), etFeedback);
				sendFeedback();
			}
		});
		btCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dismiss();
			}
		});

		etFeedback.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {
				if (etFeedback.getText().toString().trim().length() > 0) {
					btPost.setEnabled(true);
				} else {
					btPost.setEnabled(false);
				}
			}
		});
	}

	protected void sendFeedback() {
		String content = etFeedback.getText().toString();
		QBCustomObject newRecord = new QBCustomObject(
				Common.FeedbackObject.FEEDBACK_CLASS);
		newRecord.put(Common.FeedbackObject.CONTENT, content);
		newRecord.put(Common.FeedbackObject.STATE, 1);
		QBPermissions permissions = new QBPermissions();
		permissions.setDeletePermission(QBPermissionsLevel.OPEN);
		permissions.setReadPermission(QBPermissionsLevel.OPEN);
		permissions.setUpdatePermission(QBPermissionsLevel.OPEN);
		newRecord.setPermission(permissions);
		QBCustomObjects.createObject(newRecord,
				new QBEntityCallbackImpl<QBCustomObject>() {
					@Override
					public void onSuccess(QBCustomObject object, Bundle params) {
						KiraGameLog.d(">>> sendFeedback: " + object);
					}

					@Override
					public void onError(List<String> errors) {
						KiraGameLog.d("sendFeedback error " + errors);
					}
				});
		dismiss();

	}

	private void findViewIds(View view) {
		etFeedback = (EditText) view.findViewById(R.id.et_feedback);
		btPost = (Button) view.findViewById(R.id.bt_post);
		btCancel = (Button) view.findViewById(R.id.bt_cancel);
	}
}
